//
//  GroupController.m
//  Groups
//
//  Created by Mathieu on 25/07/06.
//  Copyright 2006 __MyCompanyName__. All rights reserved.
//

#import "GroupController.h"
#import "Group.h"
#import "ManualGroupMO.h"
#import "SmartGroup.h"

@implementation GroupController


- (NSArray *)sortDescriptor
{
	if(sortDescriptor == nil){
		sortDescriptor = [NSArray arrayWithObject:[[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO]];
	}
	
	return sortDescriptor;
}

- (void)setSortDescriptor:(NSArray *)newSortDescriptor
{
	sortDescriptor = newSortDescriptor;
}


-(IBAction)setSelectedSmartGroupPredicate:(id)sender{
	if([[groups selectedObjects] count] == 0){
		NSLog(@"No Group Selected");
		//noop
	}else{
		if([[[groups selectedObjects] objectAtIndex:0] isManualGroup]){
			//manualgroup so noop
		}
		else{
			NSString *newPredicateString = [predicateField stringValue];
			SmartGroup *theSmartGroup = [[groups selectedObjects] objectAtIndex:0];
			[theSmartGroup setValue:newPredicateString forKey:@"extraPredicateString"]; //save this for later (ie when the language changes).
			//attach the constant Language Binding predicate to the newpredicate string
			NSString *languageName = [[[languageArray selectedObjects] objectAtIndex:0] valueForKey:@"name"];
			NSString *constantPredicateAndNewPredicateString = [NSString stringWithFormat:@"language.name like[c] '%@' %@", languageName, newPredicateString];
			NSPredicate *newPredicate = [NSPredicate predicateWithFormat:constantPredicateAndNewPredicateString];
		//	[theSmartGroup setPredicate:newPredicate];
		}
	}
}



// remove the currently selected group object
//-(IBAction)removeSelectedGroup:(id)sender{
//	//I will have to get the selection, search for it in the appropriate array, and remove it with the array controller's remove method
//	Group *selectedGroup = [[groups selectedObjects] objectAtIndex:0];
//	NSLog(@"Selected Object that is set for deletion is: %@, \n\n %@", [selectedGroup valueForKey:@"name"], selectedGroup);
//	
//		if([selectedGroup isThisGroupTheLibrary]){
//			NSBeep();
//			NSLog(@"Cannot delete the library.");
//		}
//		else{
//			[selectedGroup setValue:nil forKey:@"language"];
//			[groups removeObject:selectedGroup];
//			[[[NSApp delegate] managedObjectContext] deleteObject:selectedGroup];
//		}
//
//}

//Possible optimisations
//Keep the selected group in a variable that is updated only when something changes, so that it doesn't have to be recalcualted each time around.
//issues. Will The code in removeWord work by removing the object from the moc entirely, only from this set relationship? If we're lucky then it will only be for this set.


@end
